﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Collections;
//using Macrobject.NObject;

namespace Common
{
    /// <summary>
    /// 反射帮助类。用的不多。
    /// </summary>
    public static class ReflectHelper
    {
        public static T Create<T>()
        {
            try
            {
                Type type = typeof(T);
                if (type.IsArray)
                {
                    return default(T);
                }
                return (T)type.Assembly.CreateInstance(type.FullName);
            }
            catch { return default(T); }
        }

        /// <summary>
        /// 得到属性值的集合。
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<string> GetPropertiesList(Type type)
        {
            List<string> liRet = new List<string>();
            foreach (PropertyInfo pi in type.GetProperties())
            {
                liRet.Add(pi.Name);
            }
            return liRet;
        }

        /// <summary>
        /// 返回 Hashtable 类型的属性集合. 以 属性名字 为键 ,以属性类型为 值.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Hashtable GetPropertiesHash(Type type)
        {
            Hashtable ht = new Hashtable();
            foreach (PropertyInfo pi in type.GetProperties())
            {
                ht[pi.Name] = pi.PropertyType;
            }
            return ht;
        }

        /// <summary>
        /// 反射得到某个值。
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="PropertyName"></param>
        /// <returns></returns>
        public static object GetValue(object obj, string PropertyName)
        {
            foreach (PropertyInfo item in obj.GetType().GetProperties())
            {
                if (item.Name == PropertyName)
                {
                    return item.GetValue(obj, null);
                }
            }
            return null;
        }

        /// <summary>
        /// 通过 反射浅克隆。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T SimpleClone<T>(this T obj)
        {
            Type t = obj.GetType();
            PropertyInfo[] properties = t.GetProperties();

            Object p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, obj, null);

            foreach (PropertyInfo pi in properties)
            {
                if (pi.CanWrite)
                {
                    pi.SetValue(p, pi.GetValue(obj, null), null);
                }
            }

            return (T)p;
        }

        //public static void SetFrom<T>(this Control obj, T con) where T : Control
        //{
        //    Type t = con.GetType();
        //    PropertyInfo[] properties = t.GetProperties();
        //    PropertyInfo[] objPs = obj.GetType().GetProperties();
        //    //Object p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, obj, null);

        //    foreach (PropertyInfo pi in properties)
        //    {
        //        if (objPs.Select(o => o.Name).Contains(pi.Name) == false) continue;

        //        if (pi.CanWrite)
        //        {
        //            pi.SetValue(obj, pi.GetValue(con, null), null);
        //        }
        //    }
        //}
    }
}